package com.example.hangzhou.welbeing.mytestdemoapp.activity;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hangzhou.welbeing.mytestdemoapp.R;
import com.example.hangzhou.welbeing.mytestdemoapp.adpater.MyRecyclerViewAdapter;
import com.example.hangzhou.welbeing.mytestdemoapp.utils.Utils;

import java.util.ArrayList;

/**
 * 杭州惟君医疗科技有限公司10
 * Created by 20433 on 2018/3/1
 */

public class AppBarActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private NavigationView navigationView;
    private TextView mTextView;
    private DrawerLayout drawer;
    private Toolbar toolbar;
    private ActionBarDrawerToggle toggle;
    private RecyclerView recyclerView;
    private FloatingActionButton fab;
    private AppBarLayout appbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_bar);
        findview();
        initview();


    }

    private void initview() {
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.menu);
        //把开关和DrawerLayout关联
        toggle = new ActionBarDrawerToggle(this, drawer, 0, 0);
        //StatusBarUtil.setColorNoTranslucentForDrawerLayout(this,drawer, Color.parseColor("5609aa"));
        //让图片就是显示他本身的颜色
        navigationView.setItemIconTintList(null);
        // 设置菜单文字的颜色选择器
        ColorStateList csl = getResources().getColorStateList(R.color.navigation_menu_item_color);
        navigationView.setItemTextColor(csl);
        navigationView.setNavigationItemSelectedListener(this);
        recyclerViewFitData();

    }

    private void recyclerViewFitData() {
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        MyRecyclerViewAdapter  mAdapter = new MyRecyclerViewAdapter(getData());
        // 设置布局管理器
        recyclerView.setLayoutManager(mLayoutManager);
        // 设置adapter
        recyclerView.setAdapter(mAdapter);
    }

    private void findview() {
        Utils.setStatusBar(this, false, false);
        Utils.setStatusTextColor(false, this);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        toolbar = (Toolbar) findViewById(R.id.app_bar_main_toolbar);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        mTextView = (TextView) findViewById(R.id.textView);
        recyclerView = (RecyclerView) findViewById(R.id.app_bar_main_recyclerView);
        fab = (FloatingActionButton) findViewById(R.id.app_bar_main_fab);
        appbar = (AppBarLayout) findViewById(R.id.app_bar_main_appbar);


    }

    private ArrayList<String> getData() {
        ArrayList<String> data = new ArrayList<>();
        String temp = " item";
        for (int i = 0; i < 20; i++) {
            data.add(i + temp);
        }
        return data;
    }


    /**
     * @desc Toolbar 三点设置必须重写这个方法
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    /**
     * @desc Toolbar 三点设置的弹出菜单的点击事件重写这个方法
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                Toast.makeText(AppBarActivity.this, "action_settings", Toast.LENGTH_LONG).show();
                break;
            case R.id.action_search:
                Toast.makeText(AppBarActivity.this, "点击事件重写这个方法", Toast.LENGTH_LONG).show();
                break;
            case R.id.action_share:
                Toast.makeText(AppBarActivity.this, "action_share", Toast.LENGTH_LONG).show();
                break;
            case android.R.id.home:
                toggle.onOptionsItemSelected(item);
                break;
        }
        return true;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        String string = null;
        switch (item.getItemId()) {
            case R.id.nav_me:
                string = "我";
                break;

            case R.id.nav_manage:
                string = "通知";
                break;
            case R.id.nav_night:
                string = "夜间模式";
                break;
            case R.id.nav_notification:
                string = "通知";
                break;
            case R.id.nav_setting:
                string = "设置";
                break;
        }
        if (!TextUtils.isEmpty(string))
            mTextView.setText("你点击了" + string);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){

            finish();
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        }

        return super.onKeyDown(keyCode, event);//继续执行父类其他点击事件
    }
}
